/**
 * 
 */
({
	launch : function() {
		var me = this;
		var perms = this.perms;
		var source = {
			browse : function() {
				var _this = this;
				var datagrid = $("<table></table>");
				var tools = $('<div></div>');
				if(perms.add) {
					var addButton = $('<a href="#" class="icon-add"></a>').appendTo(tools);
					addButton.click(function() {
						_this.add(datagrid);
						return false;
					});
				}
				if(perms.remove) {
					var rmButton = $('<a href="#" class="icon-remove"></a>').appendTo(tools);
					rmButton.click(function() {
						$.messager.confirm("请确认：", "确认要删除所选资源吗？", function(isOk) {
							if(isOk) {
								var items = datagrid.datagrid("getChecked");
								var uris = [];
								$(items).each(function() {
									uris.push(this.uri);
								});
								$.messager.mask().text("操作执行中，请稍后……");
								$.post(me.ctx + "/manager/source/extra/remove", {uris : uris}, function(data) {
									if(data.success) {
										rmButton.hide();
										datagrid.datagrid("reload");
									}
									$.messager.mask('close');
								}, "json");							
							}
						});
						return false;
					});
				}
				var win = me.createWindow('<div></div>', {
					title:"资源管理",
					width:550,
					height:300,
					tools : tools
				});
				var types = {
					"login" : "需要登录",
					"root" : "需要ROOT用户"
				};
				datagrid.appendTo(win);
				datagrid.datagrid({
					fit : true,
					url : me.ctx + "/manager/source/extra/browse",
					pagination : true,
					pageSize : 10,
					selectOnCheck : false,
					checkOnSelect : false,
					singleSelect : true,
				    columns : [[
				    	{field : 'checkbox', checkbox : true},
				        {field : 'uri', title : '资源链接', width:200},
				        {field : 'per_id', title : '过滤条件', width:120, align:"center", formatter:function(value,row,index) {
				        	if(value) {
				        		return types[value];
				        	}
				        	return "";
				        }}
				    ]],
				    onClickCell : function(index, field, value) {
				    	if(field === "source") {
				    		var rows = datagrid.datagrid("getRows");
				    		source.browse(rows[index]);
				    	}
				    },
				    onCheckAll : function(rows) {
				    	if(rows.length > 0 && perms.remove) {
				    		rmButton.show();
				    	}
				    },
				    onUncheckAll : function(rows) {
				    	if(perms.remove) {
				    		rmButton.hide();
				    	}
				    },
				    onCheck : function(index, row) {
				    	if(perms.remove) {
				    		rmButton.show();
				    	}
				    },
				    onUncheck : function(index, row) {
				    	if(perms.remove && $(this).datagrid("getChecked").length < 1) {
				    		rmButton.hide();
				    	}
				    }, 
				    onLoadSuccess : function() {
				    	if(perms.remove) {
				    		rmButton.hide();
				    	}
				    }
				});
				return win;
			},
			
			add:function(datagrid) {
				var _this = this;
				var footer = $('<div style="padding:5px;text-align:right;"></div>');
				var win = me.createWindow('<div></div>', {
					title:"资源添加",
					width:300,
					height:120,
					resizable:false,
					minimizable:false,
					maximizable:false,
					modal:true,
					footer:footer
				});
				var form = $('<form style="width:100%;" method="post"></form>').appendTo(win);
				form.form({
					url : me.ctx + "/manager/source/extra/add",
					success: function(data){
						$.messager.mask("close");
				        var data = eval('(' + data + ')');
				        if (data.success){
				        	datagrid.datagrid("options").pageNumber = 1;
				            datagrid.datagrid("reload");
				            win.window("close");
				        } else {
				        	$.messager.alert('提示：', data.message, "info");
				        }
				    }
				});
				var table = $('<table width="100%" class="form-table"></table>').appendTo($('<div style="padding:5px 5px;"></div>').appendTo(form));
				var row = table.row();
				row.cell("资源URI：", {width:"60px"});
				var field = $('<input type="text" name="uri" />');
				row.cell(field);
				field.textbox({
					required: true,
					validType : ['uri', 'maxlength[200]']
				});
				row = table.row();
				row.cell("过滤条件：");
				field = $('<select name="per_id" style="width:70%;"></select>');
				row.cell(field);
				field.combobox({
					required: true,
				    valueField:'id',
				    textField:'text',
				    data:[{
				    	id:"login",
				    	text:"需要登录"
				    }, {
				    	id:"root",
				    	text:"需要ROOT用户"
				    }]
				});
				var saveButton = $('<a href="#">提交</a>').appendTo(footer);
				saveButton.linkbutton({
					iconCls : 'icon-ok',
					onClick : function() {
						if(form.form("validate")) {
							$.messager.mask().text("操作执行中，请稍后……");
							form.form("submit");
						}
						return false;						
					}
				});
				footer.append("&nbsp;");
				var cancelButton = $('<a href="#">取消</a>').appendTo(footer);
				cancelButton.linkbutton({
					iconCls: 'icon-cancel',
					onClick: function(){
						win.window("close");
						return false;						
					}
				});
			}
		};
		source.browse();
	}
})